package com.zahb.qadx.ui.activity;

import android.content.Context;
import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.ui.fragment.HasCompleteFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huwei.
 * Data 2020-10-12.
 * Time 09:34.
 */
public class MyCourseActivity extends BaseActivity{
    private ViewPager2 courseViewpager;
    private TabLayout courseTablayout;
    private List<String> courseTablist;
    private List<Fragment>courseVpList;
    private CourseAdapter pagerAdapter;
    FragmentManager fragmentManager;

    @Override
    protected int getLayout() {
        return R.layout.activity_mycourse;
    }

    @Override
    protected void initViews() {
        courseTablayout = findViewById(R.id.tab_course);
        courseViewpager = findViewById(R.id.vp_course);

        courseTablist=new ArrayList<>();
        courseVpList=new ArrayList<>();
        courseTablist.add("待完成");
        courseTablist.add("已完成");
        courseTablist.add("过期");
        HasCompleteFragment notCompleteFragment=new HasCompleteFragment();
        HasCompleteFragment hasCompleteFragment=new HasCompleteFragment();
        HasCompleteFragment overdueFragment=new HasCompleteFragment();
        courseVpList.add(notCompleteFragment);
        courseVpList.add(hasCompleteFragment);
        courseVpList.add(overdueFragment);
        fragmentManager= getSupportFragmentManager();
        pagerAdapter=new CourseAdapter(fragmentManager,getLifecycle(),courseVpList);
        courseViewpager.setAdapter(pagerAdapter);
        courseViewpager.setOffscreenPageLimit(2);
        TabLayoutMediator mediator = new TabLayoutMediator(courseTablayout, courseViewpager,
                (tab, position) -> tab.setText(courseTablist.get(position)));
        mediator.attach();
    }

    @Override
    protected int getTitleStringRes() {
        return R.string.my_course;
    }

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, MyCourseActivity.class);
        context.startActivity(intent);
    }

    public class CourseAdapter extends FragmentStateAdapter {

        List<Fragment>fragments;

        CourseAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle, List<Fragment> fragments) {
            super(fragmentManager, lifecycle);
            this.fragments = fragments;
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return fragments.get(position);
        }

        @Override
        public int getItemCount() {
            return fragments.size();
        }
    }
}
